

//
//  AddStudentViewController.m
//  BOSS-SOOC
//
//  Created by apple on 16/9/1.
//  Copyright © 2016年 Gcx. All rights reserved.
//
#define colorbdbdbd [UIColor colorWithRed:189.0/255 green:189.0/255 blue:189.0/255 alpha:1.0f]

#import "AddStudentViewController.h"
#import "MMLabel.h"
#import "SC_TextFeild.h"
#import "SCTypeBtn.h"

@interface AddStudentViewController(){

    NSString        *_kcid;
    SC_TextFeild    *_nameT;
    SC_TextFeild    *_numberT;
    SC_TextFeild    *_classT;
}

@end

@implementation AddStudentViewController

- (void)viewDidLoad{

    [super viewDidLoad];
    
    [self.navbar setTitle:@"添加学生"];
    
    self.view.backgroundColor = BGC;
    
    [self creatMainUI];
}

- (void)creatMainUI{

    UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 80, SIZEWIDTH, 165)];
    
    mainView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:mainView];
    
    for (int i = 1; i < 4; i++) {
        
        UIView *xian = [[UIView alloc]initWithFrame:CGRectMake(ScaleSize(33), 55*i, SIZEWIDTH - ScaleSize(33), 0.5)];
        xian.backgroundColor = colordbdbdb;
        [mainView addSubview:xian];
        if (i == 3) {
            
            xian.frame = CGRectMake(0, 55*i, SIZEWIDTH, 0.5);
        }
    }
    
    NSArray *arr = @[@"*姓名：",@"*学号：",@"*班级："];
    
    for (int i = 0; i < 3; i++) {
        
        MMLabel *label = [[MMLabel alloc]initWithFrame:CGRectMake(13, i*55, 55, 55)];
        label.text = arr[i];
        label.font = NormalFont(42);
        label.textColor = Color333333;
        label.keyWord = @"*";
        label.keyWordColor = [UIColor orangeColor];
        [mainView addSubview:label];
    }
    
    
    for (int i = 0; i < 3; i++) {
        
        SC_TextFeild *text = [[SC_TextFeild alloc]initWithFrame:CGRectMake(55, i*55, SIZEWIDTH - 55 -ScaleSize(33), 55)];
        text.tag = 1000+i;
        text.font = NormalFont(42);
        text.textColor = Color333333;
        [mainView addSubview:text];
        
    }
    _nameT      = (SC_TextFeild *)[mainView viewWithTag:1000];
    _classT     = (SC_TextFeild *)[mainView viewWithTag:1002];
    _numberT    = (SC_TextFeild *)[mainView viewWithTag:1001];

    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.numberOfLines = 0;
    
    
    messageLabel.text = [NSString stringWithFormat:@"添加名单太麻烦？\n请登录%@，可以通过Excel上传学生名单。", @"http://textsooc.com"];
    
    //调节高度
    CGSize sizes = CGSizeMake(SIZEWIDTH - ScaleSize(33)*2, 0);
    
    CGSize labelSize = [messageLabel sizeThatFits:sizes];
    
    messageLabel.frame = CGRectMake(ScaleSize(33), 165, SIZEWIDTH - ScaleSize(66), labelSize.height);
    
    [mainView addSubview:messageLabel];
    
    messageLabel.font = NormalFont(36);
    
    messageLabel.textColor = colorbdbdbd;
    
    SCTypeBtn *succeedBtn = [[SCTypeBtn alloc]initWithFrame:CGRectMake(ScaleSize(60), CGRectGetMaxY(messageLabel.frame)+ScaleSize(96+33)+40, SIZEWIDTH - ScaleSize(120), ScaleSize(140))];
    [self.view addSubview:succeedBtn];
    [succeedBtn setTitle:@"确定" forState:UIControlStateNormal];
    [succeedBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:succeedBtn];
    succeedBtn.isCanUse = NO;
    
    _nameT.myBlock = ^(NSString *str){
        if (_nameT.text.length > 0 && _numberT.text.length > 0 &&_classT.text.length > 0 ) {
            succeedBtn.isCanUse = YES;
        } else {
            succeedBtn.isCanUse = NO;
        }
    };
    _numberT.myBlock = ^(NSString *str){
        if (_nameT.text.length > 0 && _numberT.text.length > 0 &&_classT.text.length > 0 ) {
            succeedBtn.isCanUse = YES;
        } else {
            succeedBtn.isCanUse = NO;
        }
    };
    _classT.myBlock = ^(NSString *str){
        if (_nameT.text.length > 0 && _numberT.text.length > 0 &&_classT.text.length > 0 ) {
            succeedBtn.isCanUse = YES;
        } else {
            succeedBtn.isCanUse = NO;
        }
    };
}

- (void)btnClicked:(UIButton *)sender{

    NSMutableDictionary *parms = [[NSMutableDictionary alloc]init];
    [parms setObject:@"815bffb68e2ea44e38cf16a98a5a29a4"    forKey:@"oauth_token"];
    [parms setObject:@"8e84bc64f03d73b5c75406ef1d7349f7"    forKey:@"oauth_token_secret"];
    [parms setObject:_nameT.text                            forKey:@"xm"];
    [parms setObject:_numberT.text                          forKey:@"xh"];
    [parms setObject:_classT.text                           forKey:@"bj"];
    [parms setObject:_kcid                                  forKey:@"kcid"];
    
    SCHttpRequest *request = [[SCHttpRequest alloc]initWithApi:API_addStudent params:parms];
    
    [self doPOSTConnect:request start:^(SCHttpRequest * _Nonnull req) {
        
    } success:^(SCHttpRequest * _Nonnull req, SCHttpResponse * _Nonnull resp) {
        
        if ([resp.originalDictionary[@"status"]integerValue] == 0) {
            
            [[JLActionManager sharedInstance]showHUDWithLabelText:resp.originalDictionary[@"statusCode"] imageView:SucceedImage inView:self.view];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                
                self.myBlock();
                
                [self.navigationController popViewControllerAnimated:YES];
                
            });

        } else {
            
            [[JLActionManager sharedInstance]showHUDWithLabelText:resp.originalDictionary[@"statusCode"] imageView:FailImage inView:self.view];
        }

        
    } failure:^(SCHttpRequest * _Nonnull req, NSError * _Nonnull error) {
        
    } finish:^(SCHttpRequest * _Nonnull req, BOOL success) {
        
    }];

}

- (id)getNameListKcid:(NSString *)kcid{

    _kcid = kcid;
    
    return self;
}



@end
